//
//  DaiBanWebVC.m
//  QoilPublic
//
//  Created by SunChao on 17/9/12.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "DaiBanWebVC.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
@interface DaiBanWebVC ()<UIWebViewDelegate>
{
    NSString * _Urlstr;
}
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation DaiBanWebVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.webView];
    [self requestData];
    
    
}

- (void)dealloc{
    NSLog(@"释放了");
}

- (void)requestData{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [_webView loadRequest:request];
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = // 打印异常
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        CLog(@"%@", exceptionValue);
    };
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [QViewUtility hideMBHudToView:self.view];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = // 打印异常
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        CLog(@"%@", exceptionValue);
    };
    
    __weak DaiBanWebVC *wSelf = self;
    
    self.context[@"myShare"] =^(){
        __strong DaiBanWebVC *sSelf = wSelf;
        NSLog(@"完成");
        NSLog(@"+++++++Begin Log+++++++");
        NSArray *args = [JSContext currentArguments];
        NSString *str = @"";
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal);
            str = [NSString stringWithFormat:@"%@",jsVal];
        }
        [sSelf myShare:str];
        JSValue *this = [JSContext currentThis];
        NSLog(@"this: %@",this);
        NSLog(@"-------End Log-------");
    };
    self.context[@"shareCircleFriends"] =^(){
        __strong DaiBanWebVC *sSelf = wSelf;
        NSLog(@"完成");
        NSLog(@"+++++++Begin Log+++++++");
        NSArray *args = [JSContext currentArguments];
        NSString *str = @"";
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal);
            str = [NSString stringWithFormat:@"%@",jsVal];
        }
        [sSelf myShare1:str];
        JSValue *this = [JSContext currentThis];
        NSLog(@"this: %@",this);
        NSLog(@"-------End Log-------");
    };
}
//分享微信
- (void)myShare:(NSString *)callString{
    NSLog(@"Get:%@", callString);
    // 成功回调JavaScript的方法Callback
    NSDictionary *dic = [self parseJSONStringToNSDictionary:callString];
    
    [UMSocialData defaultData].extConfig.wechatSessionData.title = dic[@"title"];
    [UMSocialData defaultData].extConfig.wechatSessionData.url = dic[@"url"];
    NSString *content = [dic objectForKey:@"digest"];
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:content image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            CLog(@"分享成功！");
        }
    }];
}
//分享朋友圈
- (void)myShare1:(NSString *)callString{
    NSLog(@"Get:%@", callString);
    // 成功回调JavaScript的方法Callback
    NSDictionary *dic = [self parseJSONStringToNSDictionary:callString];
    
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = dic[@"title"];
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = dic[@"htmlUrl"];
    NSString *content = [dic objectForKey:@"digest"];
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:content image:[UIImage imageNamed:@"loginlogo_image"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            CLog(@"分享成功！");
        }
    }];
}

-(NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
    return responseJSON;
}


//提交完成
//- (void)finishThis{

//    dispatch_async(dispatch_get_main_queue(), ^{
//        for (UIViewController *controller in self.navigationController.viewControllers){
//            if ([controller isKindOfClass:[QInfoManageVC class]]){
//                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAddInvoiceSuccess object:nil];
//
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    });
//}
//获取资质
- (NSString *)getCredentialsId1{
    if (self.credentialsId == nil) {
        return @"-1";
    }else{
        return self.credentialsId;
    }
    
}
#pragma mark - Getter/Setter
- (UIWebView*)webView {
    if (!_webView) {
        _webView = [UIWebView new];
        _webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
